<template>
  <div id="login">
    <el-form label-width="100px" :model="formValue" :rules="rules" ref="form">
      <el-form-item label="用户名" prop="username">
        <el-input type="text" v-model="formValue.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="formValue.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="submitForm">提交</el-button>
        <el-button>重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from '@/api/users'

export default {
  data() {
    return {
      formValue: {
        //用户名：admin8.密码:admin8
        username: 'admin8',
        password: 'admin',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 8, message: '长度在 4到 8 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 8, message: '长度在 5到 8 个字符', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    async submitForm() {
      // try {
      await this.$refs.form.validate()
      console.log(1)
      const {
        data: {
          data: { token },
        },
      } = await login(this.formValue)
      console.log(this)
      this.$store.commit('user/setToken', token)
      console.log(this.$store)
    },
  },
}
</script>

<style scoped>
.el-input {
  width: 200px;
}
</style>
